package util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yuisama
 * @date 2022/07/06 19:42
 * 通用工具类
 **/
public class Util {
    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 根据传入的文件大小返回不同的单位
     * 支持的单位如下 B,KB,MB,GB
     * @param size
     * @return
     */
    public static String parseSize(Long size) {
        String[] unit = {"B","KB","MB","GB"};
        int flag = 0;
        while (size > 1024) {
            size /= 1024;
            flag ++;
        }
        return size + unit[flag];
    }

    public static void main(String[] args) {
        long size1 = 4366527;
        long size2 = 682830;
        System.out.println(parseSize(size1));
        System.out.println(parseSize(size2));

    }

    public static String parseFileType(Boolean directory) {
        return directory ? "文件夹" : "文件";
    }

    public static String parseDate(Date lastModified) {
        return new SimpleDateFormat(DATE_PATTERN).format(lastModified);
    }
}